package com.andryyu.learn.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.andryu.base.entity.LearnHeaderEntity
import com.andryu.learn.databinding.FragmentThirdPageBinding
import com.andryyu.learn.adapter.LearnHeaderAdapter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject
import javax.inject.Named

@AndroidEntryPoint
class ThirdPageFragment : Fragment() {

    private var _binding: FragmentThirdPageBinding? = null

    // This property is only valid between onCreateView and onDestroyView.
    private val binding get() = _binding!!
    @Inject
    @Named("ThirdList")
    lateinit var mDataList: MutableList<LearnHeaderEntity<*>>
    private lateinit var mAdapter: LearnHeaderAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentThirdPageBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.rvOpenSource.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        mAdapter = LearnHeaderAdapter(mDataList)
        binding.rvOpenSource.adapter = mAdapter
    }

    private fun loadItems(){
        lifecycleScope.launch {
            mAdapter.updateItems(mDataList)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}